<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学生名称" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷名称" prop="paperName">
        <el-input
          v-model="queryParams.paperName"
          placeholder="请输入试卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="总得分" prop="totalScore">
        <el-input
          v-model="queryParams.totalScore"
          placeholder="请输入总得分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      border
      :data="recordsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生名称" align="center" prop="studentName" />
      <el-table-column label="试卷" align="center" prop="paperName" />
      <el-table-column label="总得分" align="center" prop="totalScore" />
      <el-table-column label="开始时间" align="center" prop="startTime">
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime">
      </el-table-column>
      <el-table-column label="总耗时" align="center" prop="spendTime">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['answer:records:edit']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生答题记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="试卷名称" prop="title">
          {{ form.title }}
        </el-form-item>
        <el-form-item label="试卷描述" prop="description">
          {{ form.description }}
        </el-form-item>
        <el-table
          border
          v-loading="loading"
          :data="form.questionsVO"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            prop="title"
          />
          <el-table-column label="题目" align="center" prop="questionText">
          </el-table-column>

          <el-table-column
            label="选项"
            align="center"
            prop="description"
            min-width="500"
          >
            <template slot-scope="scope">
              <el-table
                border
                v-loading="loading"
                :data="scope.row.choices"
                @selection-change="handleSelectionChange"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="55"
                  align="center"
                />

                <el-table-column label="选项" align="center" prop="id">
                </el-table-column>
                <el-table-column label="选项内容" align="center" prop="id">
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <el-table-column label="正确答案" align="center" prop="correctAnswer">
          </el-table-column>

          <el-table-column label="分数" align="center" prop="score">
          </el-table-column>

          <el-table-column label="学生答案" align="center" prop="questionText">
            <template slot-scope="scope">
              {{
                scope.row.studentAnswerDetails
                  ? scope.row.studentAnswerDetails.studentAnswer
                  : ""
              }}
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecords,
  getRecords,
  delRecords,
  addRecords,
  updateRecords,
} from "@/api/answer/records";

export default {
  name: "Records",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生答题记录表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        paperName: null,
        totalScore: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生答题记录列表 */
    getList() {
      this.loading = true;
      listRecords(this.queryParams).then((response) => {
        this.recordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        description: "",
        endTime: "",
        id: null,
        params: {},
        questionsVO: [
          {
            choices: [
              {
                id: null,
                isCorrect: 0,
                optionText: "",
                optionTitle: "",
                questionId: 0,
              },
            ],
            correctAnswer: "",
            id: null,
            paperId: 0,
            params: {},
            questionText: "",
            questionType: 0,
            remark: "",
            score: 0,
          },
        ],
        title: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生答题记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      let params = {
        paperId: row.paperId,
        studentId: row.studentId,
      };
      getRecords(params).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "学生答题记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除学生答题记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRecords(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "answer/records/export",
        {
          ...this.queryParams,
        },
        `records_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
